<template>
    <div>
        <div style="text-align: left">
            <el-button icon="el-icon-plus" @click="dialogFormVisible = true" >添加</el-button>
        </div>
        <el-dialog title="员工信息" :visible.sync="dialogFormVisible" @close="closeDialog" width="40%">
            <el-form :model="form">
                <el-form-item label="姓名" >
                    <el-input v-model="form.realName" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="生日" >
                    <el-date-picker
                            v-model="form.birth"
                            type="date"
                            placeholder="选择日期"
                            format="yyyy 年 MM 月 dd 日"
                            value-format="yyyy-MM-dd">

                    </el-date-picker>
                </el-form-item>
                <el-form-item label="性别" >
                    <el-radio v-model="form.sex" label="男">男</el-radio>
                    <el-radio v-model="form.sex" label="女">女</el-radio>
                </el-form-item>
                <el-form-item label="电话" >
                    <el-input v-model="form.userTel" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" >
                    <el-input v-model="form.email" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="地址" >
                    <el-input v-model="form.address" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="部门" >
                    <el-select v-model="form.deptId" placeholder="请选择" @change="fillPositions">
                        <el-option
                                v-for="item in departments"
                                :key="item.deptId"
                                :label="item.deptName"
                                :value="item.deptId">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="职位" >
                    <el-select v-model="form.positionId" placeholder="请选择" >
                        <el-option
                                v-for="item in positions"
                                :key="item.positionId"
                                :label="item.name"
                                :value="item.positionId">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="照片" >
                    <el-upload
                            class="avatar-uploader"
                            action="http://localhost:8888/image/upload"
                            :show-file-list="false"
                            :on-success="handleAvatarSuccess">
                        <img v-if="form.imgPath" :src="form.imgPath" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleSave">确 定</el-button>
            </div>
        </el-dialog>
        <div>
            <el-table
                    :data="tableData"
                    style="width: 100%">
                <el-table-column
                        label="ID">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.userId }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        label="姓名">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.realName }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        label="生日">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.birth }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        label="性别">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.sex }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        label="电话">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.userTel }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        width="200"
                        label="邮箱">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.email }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        label="地址">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.address }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        label="职位">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.positionName }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        label="部门">
                    <template slot-scope="scope">
                        <span style="margin-left: 10px">{{ scope.row.deptName }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                        label="照片">
                    <template slot-scope="scope">
                        <img :src="scope.row.imgPath" style="width: 40px;height: 40px;border-radius: 50%">
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button
                                size="mini"
                                type="danger"
                                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div>
            <el-pagination
                    background
                    layout="prev, pager, next"
                    :page-size="pageSize"
                    :current-page="current"
                    :total="total"
                    @current-change="loadData">
            </el-pagination>
        </div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                tableData: [],
                current: 1,
                total: 1,
                pageSize:10,
                dialogFormVisible:false,
                emptyForm:{userId:0,realName:"",birth:"",sex:"",userTel:"",address:"",email:"",positionId:"",deptId:"",imgPath:""},
                form:{userId:0,realName:"",birth:"",sex:"",userTel:"",address:"",email:"",positionId:"",deptId:"",imgPath:""},
                departments:[],
                positions:[]
            }
        },
        methods: {
            handleSave(){
                console.log("save:"+JSON.stringify(this.form));
                console.log("qs:"+this.$qs.stringify(this.form));
                this.dialogFormVisible = false;
                if(this.form.userId === 0){
                    this.$http.post("/user",this.form)
                        .then(res => {
                           if(res.data){
                               this.$message.info("添加成功！");
                           }
                        });
                }else{
                    this.$http.put("/user",this.$qs.stringify(this.form))
                        .then(res => {
                            if(res.data){
                                this.$message.info("更新成功！");
                            }
                        });
                }
            },
            handleEdit(index, row) {
                console.log(index, row);
                this.form = row;
                this.dialogFormVisible = true;
                this.fillPositions(this.form.deptId);
            },
            handleDelete(index, row) {
                console.log(index, row);

                this.$confirm('确定删除?')
                    .then(() => {
                        this.$http.delete("/user/"+row.id)
                            .then(res => {
                                this.$message.info("删除成功！");
                                this.tableData.splice(index,1);
                            });
                    });
            },
            handleAvatarSuccess(res, file) {
                this.form.imgPath = res;
                console.log("pic:"+ this.form.imgPath);
            },
            closeDialog(){
              this.form = JSON.parse(JSON.stringify(this.emptyForm));
            },
            loadData(current){
                this.$http.get("/user/page/"+current)
                    .then(res => {
                       if(res.data){
                           this.pageSize = res.data.size;
                           this.total = res.data.total;
                           this.current = res.data.current;
                           this.tableData = res.data.records;
                           console.log(this.tableData);
                       }
                    });
            },
            fillPositions(deptId){
                this.$http.get("/position?deptId="+deptId)
                    .then(res => {
                        this.positions = res.data;
                    });
            }
        },
        mounted(){
            this.loadData(1);

            this.$http.get("/dept")
                .then(res => {
                   this.departments = res.data;
                });
        }
    }
</script>

<style scoped>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 20px;
        color: #8c939d;
        width: 80px;
        height: 80px;
        line-height: 80px;
        text-align: center;
    }
    .avatar {
        width: 80px;
        height: 80px;
        display: block;
    }
</style>